package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.SettleAccountsCycleMapper;
import com.ybkj.daijia.server.model.SettleAccountsCycle;
import com.ybkj.daijia.server.service.SettleAccountsCycleService;
import com.ybkj.daijia.server.service.SuperviceService;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/3/2.
 */
@Service
public class DefaultSettleAccountsCycleService implements SettleAccountsCycleService {

    @Autowired
    private SettleAccountsCycleMapper settleAccountsCycleMapper;

    @Autowired
    private SuperviceService superviceService;


    @Override
    public Page<SettleAccountsCycle> queryByPage(Long company) {
        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        Integer offset = null;
        if (Pager.getPage() != null && Pager.getLimit() != null) {
            offset = Pager.getPage() * Pager.getLimit();
        }
        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();
        Long companyId = null;
        if (!principal.isCompanyType()) {
            companyId = company;
        }
        List<SettleAccountsCycle> content = settleAccountsCycleMapper
            .selectByCompany(companyId, offset, Pager.getLimit());
        Long total = settleAccountsCycleMapper.countByCompany(companyId);
        return new PageImpl<SettleAccountsCycle>(content, pageable, total);
    }

    @Override
    public SettleAccountsCycle create(SettleAccountsCycle sac) {
        //上传支付信息
        superviceService.Jszq(sac);

        //添加结算周期
        settleAccountsCycleMapper.insert(sac);
        return sac;
    }

    @Override
    public int deleteById(Long id) {
        return settleAccountsCycleMapper.deleteByPrimaryKey(id);
    }
}
